﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

originalTitle = document.title;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function onBlur() {
    document.body.className = 'blurred';
    //alert('blurred');
};
function onFocus() {
    document.body.className = 'focused';
    //alert('focused');
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function blinkingTitle(count, message, blink) {
    if (blink) {
        document.title = message;
    } else {
        document.title = originalTitle;
        count-- // decrement the number of times left to blink
//        if (typeof console != "undefined")
//            console.log("count is" + count);
    }
    if (count > 1) {
        blinkTimer = setTimeout("blinkingTitle(" + count + ", '" + message + "', " + !blink + ")", 2000);
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function playSound(soundfile) {

    document.getElementById("dummy").innerHTML =
 "<embed src=\"" + soundfile + "\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}

//function PlaySound() {
//    window.location = "Chat.wav"
//}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



function JS_OnKeyChange(e, form, ChatDivId) {
    var key = e.keyCode || e.which;
    if (key == 13) {
        JS_SendMessage(ChatDivId);
        $("#WriteMessageTextBox" + ChatDivId).val("");
        return false;
    }
    else {
        var ChatIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + ChatDivId;
        try {
            Nivdal.ChatWebService.WS_UpdateUserTyping(document.getElementById(ChatIdHiddenField).value, OnSuccess, onFailure);
        }
        catch (err) {
            //console.log(err);
        }
    }
    
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_OnKeyDown(e) {
    var key = e.keyCode || e.which;
    if (((e.shiftKey==1) && (key == 188 || key == 190)) ||
        (key == 60) || (key == 63)) {
        return false;
    }    
    return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function Test() {

    alert("Test");

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_OpenNewChatByUser(FriendId, FriendName) {
    var UserContent = new Array();
    UserContent[0] = FriendId;
    UserContent[1] = FriendName;
    //Looks for FriendId among FriendIdHiddenFieldX.Value. If found – sets focus on the correspondent WriteMessageTextBoxX and exists
    // (no need to do anything else)
    var FriendIdOnChatWindowValue = FriendIdOnChatWindow(FriendId);
    if (FriendIdOnChatWindow() != 0) {
        var WriteMessageTextBox = document.getElementById("WriteMessageTextBox" + FriendIdOnChatWindow);
        WriteMessageTextBox.focus();
    }
    else { //Call JS_FindAvailableChatDiv to get an available chat window number.
        // If 0 returns – alert the user that there is no chat windows available, and exit
        var Available_Chat_Window_Number = JS_FindAvailableChatDiv();
        UserContent[2] = Available_Chat_Window_Number;
        if (Available_Chat_Window_Number == 0)
            try {
                Nivdal.ChatWebService.WS_ReturnTranslatedString("Chat_NoRoomAvail", OnTranslatedStringSuccess, onFailure);
            }
            catch (err) {
                //console.log(err);
            }
        else { //Call WS_OpenNewChatByUser Web service function. Parameter FriendId. It gets back ChatRoomId. 
            //If chatroomId is 0 – alert the user that the friend is not available for chat, and exit
            try {
                Nivdal.ChatWebService.WS_OpenNewChatByUser(FriendId, OnChatRoomIdSucceeded, onFailure, UserContent);
            }
            catch (err) {
                //console.log(err);
            }

        }

    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Callback function invoked on successful completion of the page method.
function OnChatRoomIdSucceeded(result, UserContent) {
    //alert("OnChatRoomIdSucceeded");
    var WriteMessageTextBox = "";
    var ChatRoomId = result;
    if (ChatRoomId == 0)
        try {
            Nivdal.ChatWebService.WS_ReturnTranslatedString("Chat_FriendNotAvailForChat", OnTranslatedStringSuccess, onFailure);
//            playSound('Chat.wav'); //alert the user that  he has a chat message only for the first time
//            alert("play sound for the first time");
        }
        catch (err) {
            //console.log(err);
        }
    else { //Looks for chatroomId in the ChatIdHiddenFieldX  of the existing chat windows. 
        //If found – sets focus on the existing chatrom WriteMessageTextBoxX 
        var ExistingChatWindows = LooksForChatRoomIdInChatIdHiddenFieldX(ChatRoomId);
        if (ExistingChatWindows != 0) {
            WriteMessageTextBox = document.getElementById("WriteMessageTextBox" + ExistingChatWindows);
            WriteMessageTextBox.focus();
        }
        else { //Call JS_InitChatWindow(parameters – chat window number from 1, and ChatRoomId from 2, FriendName, FriendId)
            JS_InitChatWindow(UserContent[2], ChatRoomId, UserContent[0], UserContent[1]);
            //playSound('Chat.wav'); //alert the user that  he has a chat message only for the first time
            //Calls JS_RefreshChatDiv to receive the old messages in this chat.
            JS_RefreshChatDiv(UserContent[2]);
            WriteMessageTextBox = document.getElementById("WriteMessageTextBox" + UserContent[2]);
            WriteMessageTextBox.focus();
        }
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function onFailure(theError) {
    //console.log(theError);
    //alert("Error is: " + theError);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_InitChatWindow(ChatWindowNumber, ChatRoomId, FriendId, FriendName) {
    var MessagesLabel = "";
    var ChatIdHiddenField = "";
    var FriendNameHyperlink = "";
    var ChatDiv = "";
    var TypingGif = "";
    var LastMessageHiddenField = "";
    switch (ChatWindowNumber) {
        case 1:
            MessagesLabel = document.getElementById("ctl00_ctl00_MessagesLabel1");
            ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField1");
            FriendIdHiddenField = document.getElementById("ctl00_ctl00_FriendIdHiddenField1");
            FriendNameHyperlink = document.getElementById("ctl00_ctl00_FriendNameHyperlink1");
            ChatDiv = document.getElementById("ChatDiv1");
            TypingGif = document.getElementById("TypingGif1");
            LastMessageHiddenField = document.getElementById("ctl00_ctl00_LastMessageHiddenField1");
            //TypingDivStatus = document.getElementById("TypingDivStatus1");
            break;
        case 2:
            MessagesLabel = document.getElementById("ctl00_ctl00_MessagesLabel2");
            ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField2");
            FriendIdHiddenField = document.getElementById("ctl00_ctl00_FriendIdHiddenField2");
            FriendNameHyperlink = document.getElementById("ctl00_ctl00_FriendNameHyperlink2");
            ChatDiv = document.getElementById("ChatDiv2");
            TypingGif = document.getElementById("TypingGif2")
            LastMessageHiddenField = document.getElementById("ctl00_ctl00_LastMessageHiddenField2");
            //TypingDivStatus = document.getElementById("TypingDivStatus2");
            break;
        case 3:
            MessagesLabel = document.getElementById("ctl00_ctl00_MessagesLabel3");
            ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField3");
            FriendIdHiddenField = document.getElementById("ctl00_ctl00_FriendIdHiddenField3");
            FriendNameHyperlink = document.getElementById("ctl00_ctl00_FriendNameHyperlink3");
            ChatDiv = document.getElementById("ChatDiv3");
            TypingGif = document.getElementById("TypingGif3")
            LastMessageHiddenField = document.getElementById("ctl00_ctl00_LastMessageHiddenField3");
            //TypingDivStatus = document.getElementById("TypingDivStatus3");
            break;
        case 4:
            MessagesLabel = document.getElementById("ctl00_ctl00_MessagesLabel4");
            ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField4");
            FriendIdHiddenField = document.getElementById("ctl00_ctl00_FriendIdHiddenField4");
            FriendNameHyperlink = document.getElementById("ctl00_ctl00_FriendNameHyperlink4");
            ChatDiv = document.getElementById("ChatDiv4");
            TypingGif = document.getElementById("TypingGif4")
            LastMessageHiddenField = document.getElementById("ctl00_ctl00_LastMessageHiddenField4");
            //TypingDivStatus = document.getElementById("TypingDivStatus4");
            break;
    }

    MessagesLabel.innerHTML = "";
    FriendIdHiddenField = FriendId;
    ChatIdHiddenField.value = ChatRoomId;
    FriendNameHyperlink.innerHTML = FriendName;
    FriendNameHyperlink.href = "UserPage.aspx?Id=" + FriendId;
    TypingGif.style.display = "none"; //to hide it
    LastMessageHiddenField.value = 0;

    if (ChatRoomId == "0") {
        //alert("_ChatDiv is 0: " + _ChatDiv.style.display);
        ChatDiv.style.display = "none";
    }
    else {
        ChatDiv.style.display = "inline";
        ChatDiv.style.width = "40px";
    }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function FriendIdOnChatWindow(FriendId) {
    var FriendOnChatWindowNumber = 0;
    for (i = 1; i <= 4; i++) {
        var FriendIdHiddenField = "ctl00_ctl00_FriendIdHiddenField" + i;
        var FriendIdHiddenFieldValue = document.getElementById(FriendIdHiddenField).value;
        if (FriendIdHiddenFieldValue == FriendId) {
            FriendOnChatWindowNumber = i;
            break;
        }

        return FriendOnChatWindowNumber;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function LooksForChatRoomIdInChatIdHiddenFieldX(ChatRoomId) {
    var ReturnValue = 0;
    for (i = 1; i <= 4; i++) {
        var FriendIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + i;
        var ChatIdHiddenFieldValue = document.getElementById(FriendIdHiddenField).value;
        if (ChatIdHiddenFieldValue == ChatRoomId) {
            ReturnValue = i;
            break;
        }

    }
    return ReturnValue;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_SendMessage(ChatDivId) {

    var ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField" + ChatDivId); //Create the relevant ChatIdHiddenField string
    var ChatIdHiddenField_Value = ChatIdHiddenField.value; //get the value of it
    //alert("ChatIdHiddenField_Value is" + ChatIdHiddenField_Value);
    var WriteMessageTextBox_String = ("WriteMessageTextBox" + ChatDivId); //Build the relevat WriteMessageTextBox string
    var UserMessage = document.getElementById(WriteMessageTextBox_String); //Find it
    var CurrentUserMessage_Value = UserMessage.value; //Get the value of it

    var UserContent = ChatDivId; //for deliver in the WS
    try {
//        if (typeof console != "undefined") {
//            //console.log("ChatIdHiddenField is" + ChatIdHiddenField);
//            console.log("UserContent value is" + UserContent);
//        }
        Nivdal.ChatWebService.WS_AddChatMessage(ChatIdHiddenField_Value, CurrentUserMessage_Value, OnSendMessageChatsSucceeded, onFailure, UserContent); //update the value
    }
    catch (err) {
        //console.log(err);
    }
    //Sets scroll position in the div around MessagesLabelX to be the bottom
    //$("#MessagesDiv" + UserContent).attr({ scrollTop: $("#MessagesDiv" + UserContent).attr("scrollHeight") });
    $("#MessagesDiv" + UserContent).scrollTop($("#MessagesDiv" + UserContent)[0].scrollHeight);
    
    //clear the WriteMessageTextBox after sebding the message
    UserMessage.value = "";
    UserMessage.focus();
    return false;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Callback function invoked on successful completion of the page method.
function OnSendMessageChatsSucceeded(result, UserContent) {
    //JS_RefreshChatDiv(UserContent);
    //koko
//    if (result != "Empty") {
//        var MessageLabel = document.getElementById("ctl00_ctl00_MessagesLabel" + UserContent);
//        var YouString =  document.getElementById("ctl00_ctl00_YouHiddenField").value;
//        MessageLabel.innerHTML = MessageLabel.innerHTML + "<span style=\"color:Blue;font-weight:bold;\">" + YouString + ":</span> " + result + "<br />";
          $("#MessagesDiv" + UserContent).scrollTop($("#MessagesDiv" + UserContent)[0].scrollHeight);         
//    }      
}





//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_FindAvailableChatDiv() {
    //Finds the first available chat div that has ChatIdHiddenFieldX = 0
    var availChatDiv = 0
    for (i = 1; i <= 4; i++) {
        var ChatIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + i;
        if (document.getElementById(ChatIdHiddenField).value == 0) {
            availChatDiv = i;
            break;
        }

    }
    return availChatDiv
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function OnWSRequestFailed(error) {
    comsole.log("Stack Trace: " + error.get_stackTrace() + "\r\n" +
                    "Error: " + error.get_message() + "\r\n" +
                    "Status Code: " + error.get_statusCode() + "\r\n" +
                    "Exception Type: " + error.get_exceptionType() + "\r\n" +
                    "Timed Out: " + error.get_timedOut());
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function OnWSRequestComplete(results) {
   var  _Results = document.getElementById("TextBox1");
    if (results != null) {
        _Results.value = _Results.value + results + "\r\n";
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function handleKeyPress(e, form) {
    var key = e.keyCode || e.which;
    if (key == 13) {
        alert("Hi");
    }
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function OnSuccess(asd) {
    //console.log(asd);
    //alert(asd); //result will contain the return parameter from web service
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_RefreshChatDiv(ChatDivId) { 
    var UserContent = ChatDivId;
    var ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField" + ChatDivId);
    var LastMessageHiddenField = document.getElementById("ctl00_ctl00_LastMessageHiddenField" + ChatDivId);
    //If ChatIdHiddenFieldX.value  is not 0 – Uses WS_ReadNewMessages Web service function to receive an information about the chat 
    //(parameters: ChatIdHiddenFieldX.value and LastMessageXHiddenField.Value)
    if (ChatIdHiddenField.value != 0) {
//        if (typeof console != "undefined") {
//            console.log("ChatIdHiddenField.value is: " + ChatIdHiddenField.value);
//            console.log("LastMessageHiddenField is: " + LastMessageHiddenField.value);
//        }
        try {
            Nivdal.ChatWebService.WS_ReadNewMessages(ChatIdHiddenField.value, LastMessageHiddenField.value, OnReadNewMessagesChatsSucceeded, onFailure, UserContent);
        }
        catch (err) {
             //alert("here1" + err);
        }
        try {
            Nivdal.ChatWebService.WS_IsUserOnline(ChatIdHiddenField.value, OnIsUserOnlineSucceeded, onFailure, UserContent);
        }
        catch (err) {
            //console.log(err);
        }
         
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Callback function invoked on successful completion of the page method.
//Receive new messages for a given chat div, and add it to the existing MessagesLabelX of that div.
function OnReadNewMessagesChatsSucceeded(result, UserContent) {
//    if (typeof console != "undefined") {
//        console.log(" is: " + result);
//    }
    if (result.length > 0) {
        var NewChatArray = result.split("<>", 3);
//        if (typeof console != "undefined") {
//            console.log(NewChatArray[0]);
//            console.log(NewChatArray[1]);
//            console.log(NewChatArray[2]);
        //        }

     if ((NewChatArray[2] == 'True') && (document.body.className === 'blurred')) {
            playSound('Chat.wav');
            Nivdal.ChatWebService.WS_ReturnTranslatedString("Chat_TitleMessage", OnTranslatedBlinkingTitleStringSuccess, onFailure);
        }
        var LastMessageHiddenField = document.getElementById("ctl00_ctl00_LastMessageHiddenField" + UserContent);
        LastMessageHiddenField.value = NewChatArray[0];
        var MessagesLabel = document.getElementById("ctl00_ctl00_MessagesLabel" + UserContent);
        MessagesLabel.innerHTML = MessagesLabel.innerHTML + NewChatArray[1];
        //$("#MessagesDiv" + UserContent).attr({ scrollTop: $("#MessagesDiv" + UserContent).attr("scrollHeight") });
        
        $("#MessagesDiv" + UserContent).scrollTop($("#MessagesDiv" + UserContent)[0].scrollHeight + 100);

        
        //        var MessagesLabel = document.getElementById("ctl00_ctl00_MessagesLabel" + UserContent);
        //        var Message = MessagesLabel.innerHTML;
        //        Message = Message + result;
        //        MessagesLabel.innerHTML = Message;
        //        if (document.body.className === 'blurred') {
        //            playSound('Chat.wav');
        //            Nivdal.ChatWebService.WS_ReturnTranslatedString("Chat_TitleMessage", OnTranslatedBlinkingTitleStringSuccess, onFailure);

    }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function OnTranslatedBlinkingTitleStringSuccess(result) {

    blinkingTitle(3, result, true); ;
}




//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function OnTranslatedStringSuccess(result) {

    alert(result);
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function OnIsUserOnlineSucceeded(result, UserContent) {
 var ChatDiv = document.getElementById("ChatDiv" + UserContent);
 var IsOnlineImage = document.getElementById("ctl00_ctl00_IsOnlineImage" + UserContent);
 var IsOfflineImage = document.getElementById("ctl00_ctl00_IsOfflineImage" + UserContent);
 if (result == "true") {
     IsOnlineImage.style.display = "block"; //to show it
     IsOfflineImage.style.display = "none"; //to hide it
 }
 else {
     IsOfflineImage.style.display = "block"; //to show it
     IsOnlineImage.style.display = "none"; //to hide it
 }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_ChatRefresh() {
    // Calls WS_LookForNewChats web service, giving it all the existing chatroomIds 
    //(CHAT_NUMBER_OF_ROOMS parameters – all taken from ChatIdHiddenFieldX.Value)
    try {
        Nivdal.ChatWebService.WS_LookForNewChats(document.getElementById("ctl00_ctl00_ChatIdHiddenField1").value, document.getElementById("ctl00_ctl00_ChatIdHiddenField2").value, document.getElementById("ctl00_ctl00_ChatIdHiddenField3").value, document.getElementById("ctl00_ctl00_ChatIdHiddenField4").value, OnLookForNewChatsSucceeded, onFailure);
    }
    catch (err) {
        //console.log(err);
    }
    //If ChatRoomId  is not 0 – div to be visible
    //ChatIdHiddenFieldX.Value only to them do the JS_RefreshChatDiv();
      
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Callback function invoked on successful completion of the page method.
function OnLookForNewChatsSucceeded(result) {
    var labelmessage = document.getElementById("ctl00_ctl00_MenuContentPlaceholder_MessageLabel");
    var CHAT_REFRESH_SECONDS = 2000;
    var ChatIdHiddenField = "";
    var FriendNameHyperlink = "";
    //alert("ChatRoomId  in OnLookForNewChatsSucceeded is: " + result);
    var NewChatArray = result.split(",", 3);
    var ChatRoomId = NewChatArray[0];
    if (ChatRoomId != 0) {
       var NewWindowNumber = JS_FindAvailableChatDiv();
       ChatIdHiddenField = document.getElementById("ctl00_ctl00_ChatIdHiddenField" + NewWindowNumber);
       ChatIdHiddenField.value = ChatRoomId ;
       var ChatDiv_Pannel = document.getElementById("ChatDiv" + NewWindowNumber);
       var friendName = NewChatArray[1];
       var friendId = NewChatArray[2];
       FriendNameHyperlink = document.getElementById("ctl00_ctl00_FriendNameHyperlink" + NewWindowNumber);
       FriendNameHyperlink.innerHTML = friendName;
       FriendNameHyperlink.href = "UserPage.aspx?Id=" + friendId;
       ChatDiv_Pannel.style.display = "inline"; //to show it
       //JS_RefreshChatDiv(NewWindowNumber); //Called anyway next time the loop will be running -- consult with shaul
       }
       for (i = 1; i <= 4; i++) {
           ChatIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + i;
           ChatRoomId = document.getElementById(ChatIdHiddenField).value;
           //alert(document.getElementById(ChatIdHiddenField).value);
           if (ChatRoomId != 0) {
               //labelmessage.innerHTML = ChatRoomId;
               //alert(ChatRoomId );
               JS_RefreshChatDiv(i);
               checkFriendTyping(ChatRoomId, i);
           }
   }
//   CHAT_REFRESH_SECONDS += new Date().getTime();
//   while (new Date() < CHAT_REFRESH_SECONDS) { }
   //   JS_ChatRefresh();

  
   
   //labelmessage.innerHTML = ChatRoomId;

   setTimeout("JS_ChatRefresh()", CHAT_REFRESH_SECONDS);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkFriendTyping(ChatRoomId, i) {
    //alert("checkFriendTyping");
    var UserContent = i;
    try {
        Nivdal.ChatWebService.WS_ReadUserTypingInfo(ChatRoomId, OnTypingSucceeded, onFailure, UserContent);
    }
    catch (err) {
        //console.log(err);
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function OnTypingSucceeded(result, UserContent) {

    var TypingGif = document.getElementById("TypingGif" + UserContent);
//    var labelmessage = document.getElementById("ctl00_ctl00_MenuContentPlaceholder_MessageLabel");
//    labelmessage.innerHTML = labelmessage.innerHTML + "<br/> result is: " + result;
    //var TypingDivStatus = document.getElementById("TypingDivStatus" + UserContent);
    if (result == true) {
        //TypingDivStatus.style.display = "block"; //to show it
        TypingGif.style.display = "block"; //to show it
    }
    else
        //TypingDivStatus.style.display = "none"; //to hide it
         TypingGif.style.display = "none"; //to hide it
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_CloseChat(ChatDivId) {
    var ChatIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + ChatDivId;
    var ChatRoomId = document.getElementById(ChatIdHiddenField).value;
//    var IsOnlineImage = document.getElementById("ctl00_ctl00_IsOnlineImage" + UserContent);
//    var IsOfflineImage = document.getElementById("ctl00_ctl00_IsOfflineImage" + UserContent);
//    IsOnlineImage.style.display = "block"; //to show it
//    IsOfflineImage.style.display = "none"; //to hide it
    JS_InitChatWindow(ChatDivId, "0", "0", "0");
    //alert("ChatRoomId  in JS_CloseChat is: " + ChatRoomId );
    //Nivdal.ChatWebService.WS_CloseChatByUser(ChatRoomId , OnCloseChatSucceeded);
    try {
        Nivdal.ChatWebService.WS_CloseChatByUser(ChatRoomId, OnSuccess, onFailure);
    }
    catch (err) {
        //console.log(err);
    }
    
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_InitAllChatWindows() {
    var ChatDiv = "";
    for (i = 1; i <= 4; i++) {
        var ChatIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + i;
        //alert(document.getElementById(ChatIdHiddenField).value);
        var ChatRoomId = document.getElementById(ChatIdHiddenField).value;
        if (ChatRoomId == 0) {
            ChatDiv = document.getElementById("ChatDiv" + i);
            ChatDiv.style.display = "none"; //to hide it
        }
        if (ChatRoomId != 0) {
            //alert("JS_InitAllChatWindows " + ChatRoomId);
            ChatDiv = document.getElementById("ChatDiv" + i);
            ChatDiv.style.display = "inline"; //to show it
            // to set the height:
            //$("#MessagesDiv" + i).attr({ scrollTop: $("#MessagesDiv" + i).attr("scrollHeight") });
            $("#MessagesDiv" + i).scrollTop($("#MessagesDiv" + i)[0].scrollHeight);
            
           
        }
    }

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JS_UpdateUserTyping(ChatDivId) {
    var ChatIdHiddenField = "ctl00_ctl00_ChatIdHiddenField" + ChatDivId;
    var ChatRoomId = document.getElementById(ChatIdHiddenField).value;
    try {
        Nivdal.ChatWebService.WS_UpdateUserTyping(ChatRoomId, OnSuccess, onFailure)
    }
    catch (err) {
        //console.log(err);
    }

}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function disableEnterKey(e) {
    var key = e.keyCode || e.which;
    //alert("Key is: "+ key );
    return (key != 13);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function cancel(e) {
    var key;
    if (window.event) key = window.event.keyCode; // Internet Explorer
    else key = e.which;
    //alert(key);
    if (key == 13)
        return false; // IE style
}